package Merlion.userManagement.bean;

import Merlion.userManagement.entity.RoleEntity;
import Merlion.userManagement.entity.SystemUserEntity;
import Merlion.util.helper.PasswordHash;
import Merlion.util.state.SystemUserState;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class RegistrationBean implements RegistrationBeanRemote {

    @PersistenceContext
    private EntityManager em;

    @Override
    public List<SystemUserState> viewAllUser() {
        System.out.println("**registrationBean : viewAllUser()");

        Query q = em.createQuery("SELECT c FROM SystemUserEntity c");
        List<SystemUserState> stateList = new ArrayList<SystemUserState>();
        
        for (Object o : q.getResultList()) {
            SystemUserEntity c = (SystemUserEntity) o;
            if (c.isAccountStatus() == false) {
                SystemUserState suState = new SystemUserState(c.getEmail());
                // System.out.println(c.getEmail());
                stateList.add(suState);
            }
        }
        return stateList;
    }

    @Override
    public boolean createNewUser(String email, String name, String password, List<String> roles, String address,
            String phoneNumber, String gender, String city, String country, String securityQuestion,
            String securityAnswer) {

        System.out.println("**RegistrationBean: createNewUser()");
        SystemUserEntity systemUser = em.find(SystemUserEntity.class, email);

        try {
            if (systemUser == null) {
                systemUser = new SystemUserEntity();
                String md5password = PasswordHash.MD5(password + email);
                ArrayList a = new ArrayList();

                systemUser.setEmail(email);
                systemUser.setName(name);
                systemUser.setPassword(md5password);
                systemUser.setAddress(address);
                systemUser.setPhoneNumber(phoneNumber);
                systemUser.setGender(gender);
                systemUser.setCity(city);
                systemUser.setCountry(country);
                systemUser.setSecurityQuestion(securityQuestion);
                systemUser.setSecurityAnswer(securityAnswer);
                em.persist(systemUser);
                em.flush();

                for (Object o : roles) {
                    String roleName = (String) o;
                    RoleEntity rolesEntity = new RoleEntity();
                    rolesEntity.setRoles(roleName);
                    List<SystemUserEntity> systemUserList = new ArrayList<SystemUserEntity>();
                    rolesEntity.setSystemUserList(systemUserList);
                    em.persist(rolesEntity);
                    List<SystemUserEntity> userList = rolesEntity.getSystemUserList();
                    userList.add(systemUser);
                    em.flush();
                    a.add(rolesEntity);
                }

                systemUser.setRoles(a);
                em.merge(systemUser);
                em.flush();

                System.out.println("**RegistrationBean: User are successfully registered.");
                return true;
            } else {
                System.out.println("**RegistrationBean: User has already been registered before. Registration failed.");
                return false;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }
}
